package com.xtc.netty.protocol.http.xml.handler;

import com.xtc.netty.protocol.http.xml.HttpXmlRequest;
import com.xtc.netty.protocol.http.xml.HttpXmlResponse;
import com.xtc.netty.protocol.http.xml.OrderFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author Administrator
 */
public class HttpXmlClientHandler extends SimpleChannelInboundHandler<HttpXmlResponse> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        HttpXmlRequest request = new HttpXmlRequest(null, OrderFactory.create(11));

        ctx.writeAndFlush(request);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpXmlResponse response) throws Exception {
        if (response.getResponse().decoderResult().isSuccess()) {
            System.out.println("The client receive response of http header is: " + response.getResponse().headers().names());
            System.out.println("The client receive response of http body is: " + response.getBody());
        } else {
            System.out.println("客户端解码失败");
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
